package com.zhoutao.blog.seven.pages;

import com.zhoutao.blog.seven.model.PageList;
import com.zhoutao.blog.seven.model.detail.ArticleDetail;
import com.zhoutao.blog.seven.service.ArticleService;
import com.zhoutao.blog.seven.service.query.ArticleQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/tech-weekly")
@RequiredArgsConstructor
public class TechWeeklyPageController extends AbstractPageController {

    private final ArticleService articleService;

    @GetMapping
    public String index(Model model,
                        @RequestParam(defaultValue = "1", required = false) Integer page,
                        @RequestParam(defaultValue = "5", required = false) Integer limit) {
        packageBaseInfo(model);

        final ArticleQuery query = new ArticleQuery();
        query.setType("TEACH");

        final PageList<ArticleDetail> pageList = articleService.find(query, page, limit);
        String pre = String.format("/tech-weekly?page=%s", page - 1);
        String next = String.format("/tech-weekly?page=%s", page + 1);
        pageList.setPrePage(pre).setNextPage(next);
        model.addAttribute("content", pageList);
        return "techWeekly";
    }
}
